package controllayer;

import java.util.ArrayList;

import dblayer.DBCity;
import dblayer.DBConnection;
import dblayer.IFDBCity;
import modellayer.City;

public class CtrCity {

	private IFDBCity dbObj;
	
	public CtrCity(){
		dbObj = new DBCity();
	}
	
	public ArrayList<City> getAllCities(){
		ArrayList<City> list = new ArrayList<City>();
		list = dbObj.getAllCities();
		return list;
	}
	
	public City searchByZipCode(int zipCode){
		return dbObj.searchCityByZip(zipCode);
	}
	
	public City searchByName(String name){
		return dbObj.searchCityByName(name);
	}
	
	//
	
	public int updateCity(int zipCode, String city){
		City cityObj = new City();
		
		cityObj.setCodeZip(zipCode);
		cityObj.setCity(city);
		
		return dbObj.updateCity(cityObj);
	}
	
	public void createCity(int zipCode, String city){
		City cObj = new City();
		
		cObj.setCodeZip(zipCode);
		cObj.setCity(city);
		
		try{
			DBConnection.startTransaction();
			dbObj.insertCity(cObj);
			DBConnection.commitTransaction();
		}
		catch(Exception e){
			DBConnection.rollbackTransaction();
		}
		
		
	}
	
	public int deleteCity(int zipCode){
		return dbObj.deleteCity(zipCode);
	}
	
}
